<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId'.$cfgPack])
  || (isset($_SESSION['s_userType'.$cfgPack]) && isset($cfgFileNames['dailyreportmaster'][$_SESSION['s_userType'.$cfgPack]]) 
      && $cfgFileNames['dailyreportmaster'][$_SESSION['s_userType'.$cfgPack]] == 0))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
  exit();
}
else
{
	$companyName  = "";
	$srNo         = 0;
	$delivaryDate = "";
	$address      = "";
	$userName     = "";
	$challanType  = "";
	$partySelect  = NULL;
	$party        = NULL;
	$print        = isset($_REQUEST['print']) ? $_REQUEST['print'] : 0;
  $printArray   = array();
  $msg = '<tr><td align="center" colspan="6"> <h2><font color="red"><b>Record Not Found...!</b></h2></font></td></tr>';

  // Condition runs when request comes for PARTY data : Starst
  if(isset($_REQUEST['partyRequest']) && $_REQUEST['partyRequest'] == 1)
  {
    $party = "JOIN party ON party.partyId = deliverychallanmaster.partyId";
	  $partySelect = "party.partyId,party.companyName,party.city,NULL AS partyTINNo";
  }
  
  // Condition runs when request comes for SUPPLIER data : Starst
  if(isset($_REQUEST['supplierRequest']) && $_REQUEST['supplierRequest'] == 1)
  {
    $party = "JOIN supplier ON supplier.supplierId = deliverychallanmaster.supplierId";
	  $partySelect = "supplier.supplierId,supplier.companyName,supplier.city,supplier.partyTINNo";
  }
  $selectDeliveryChalan = "SELECT deliverychallanmaster.deliveryChallanMasterId,deliverychallanmaster.address,technician.firstName,deliverychallanmaster.challanType,
                                  deliverychallanmaster.srNo,user.userId,user.userName,DATE_FORMAT(deliverychallanmaster.delivaryDate,'%d-%m-%Y') AS delivaryDate,
                                  ".$partySelect."
				  						       FROM deliverychallanmaster
					 						       JOIN user ON user.userId      = deliverychallanmaster.userId
					 						       LEFT JOIN technician ON deliverychallanmaster.technicianId = technician.technicianId
						 					            ".$party."
											      WHERE deliveryChallanMasterId = ".$_REQUEST['deliveryChallanMasterId']; 
  $selectQueryDeliveryChalan = mysql_query($selectDeliveryChalan);
	if($deliveryChalanRow = mysql_fetch_array($selectQueryDeliveryChalan))
  {
  	$deliveryChallanMasterId = $deliveryChalanRow['deliveryChallanMasterId'];
  	$companyName             = $deliveryChalanRow['companyName'];
  	$srNo                    = $deliveryChalanRow['srNo'];
  	$delivaryDate            = $deliveryChalanRow['delivaryDate'];
  	$address                 = $deliveryChalanRow['address'];
  	$firstName               = $deliveryChalanRow['firstName'];
  	$userName                = $deliveryChalanRow['userName'];
  	$challanType             = $deliveryChalanRow['challanType'];
  	//Array Variable for Print Data : Starts
  	$printArray['companyName']  = $deliveryChalanRow['companyName'];
  	$printArray['srNo']         = $deliveryChalanRow['srNo'];
  	$printArray['delivaryDate'] = $deliveryChalanRow['delivaryDate'];
  	$printArray['address']      = $deliveryChalanRow['address'];
  	$printArray['firstName']    = $deliveryChalanRow['firstName'];
  	$printArray['challanType']  = $deliveryChalanRow['challanType'];
  	$printArray['city']         = $deliveryChalanRow['city'];
  	$printArray['partyTINNo']   = $deliveryChalanRow['partyTINNo'];
  	//Array Variable for Print Data : Ends
  }
  $selectdeliverychallandetail = "SELECT deliverychallandetail.deliveryChallanDetailId,deliverychallandetail.deliveryChallanMasterId,deliverychallandetail.discription,deliverychallandetail.itemId,
                                         deliverychallandetail.subCategoryId,deliverychallandetail.quantity,deliverychallandetail.remarks,item.itemId,item.itemName,subcategory.subCategoryId,
                                         subcategory.subCategoryName
                                    FROM deliverychallandetail
                                    JOIN item        ON item.itemId = deliverychallandetail.itemId
                                    JOIN subcategory ON subcategory.subCategoryId = deliverychallandetail.subCategoryId
                                   WHERE deliverychallandetail.deliveryChallanMasterId = ".$_REQUEST['deliveryChallanMasterId']; 
  $deliverychallandetailQuery = mysql_query($selectdeliverychallandetail);
  $deliveryChalanArray = array();
  $deliveryChalanCount = 0;
  while($deliverychallandetailRow = mysql_fetch_array($deliverychallandetailQuery))
  {
    if($deliverychallandetailRow['discription'] != "")
    {
  	  $deliveryChalanArray[$deliveryChalanCount]['itemName'] = $deliverychallandetailRow['itemName']." # ".$deliverychallandetailRow['discription'];
  	}                                                        
  	else                                                     
  	{                                                        
  	  $deliveryChalanArray[$deliveryChalanCount]['itemName'] = $deliverychallandetailRow['itemName'];
  	}
  	$deliveryChalanArray[$deliveryChalanCount]['subCategoryName'] = $deliverychallandetailRow['subCategoryName'];
  	$deliveryChalanArray[$deliveryChalanCount]['quantityReturn']  = 0;
  	$deliveryChalanArray[$deliveryChalanCount]['quantity']        = $deliverychallandetailRow['quantity'];
  	$deliveryChalanArray[$deliveryChalanCount]['remarks']         = $deliverychallandetailRow['remarks'];
  	
  	$selectReturnQnty = "SELECT quantityReturn FROM goodsreturn WHERE deliveryChallanDetailId = ".$deliverychallandetailRow['deliveryChallanDetailId'];
  	$selectReturnQntyRes = mysql_query($selectReturnQnty);
  	while($row = mysql_fetch_array($selectReturnQntyRes))
  	{
  	  $deliveryChalanArray[$deliveryChalanCount]['quantityReturn'] += $row['quantityReturn'];
  	}
  	$deliveryChalanArray[$deliveryChalanCount]['netQuantity']  = $deliveryChalanArray[$deliveryChalanCount]['quantity'];
    $deliveryChalanArray[$deliveryChalanCount]['quantity'] = $deliveryChalanArray[$deliveryChalanCount]['quantity'] + $deliveryChalanArray[$deliveryChalanCount]['quantityReturn'];  	                      

    if($deliveryChalanArray[$deliveryChalanCount]['quantityReturn'] == 0)
    {
    	$deliveryChalanArray[$deliveryChalanCount]['quantityReturn'] = "";
    	$deliveryChalanArray[$deliveryChalanCount]['netQuantity'] = "";
    }
  	$deliveryChalanCount++;
  }
  if($deliveryChalanCount == 0)
  {
	  $msg = 'Record Not Found...!';
  }
  include("./bottom.php");
  $smarty->assign("msg",$msg);
  $smarty->assign("deliveryChalanArray",$deliveryChalanArray);
  $smarty->assign("companyName",$companyName);
  $smarty->assign("srNo",$srNo);
  $smarty->assign("delivaryDate",$delivaryDate);
  $smarty->assign("address",$address);
  $smarty->assign("firstName",$firstName);
  $smarty->assign("userName",$userName);
  $smarty->assign("challanType",$challanType);
  $smarty->assign("print",$print);
  $smarty->assign("printArray",$printArray);
  if($print == 1)
    $smarty->display("deliveryChalanPrint.tpl");
  else
    $smarty->display("deliveryChallanDetail.tpl");
}
?>